<?php

/**
 * Shortcut helper for CClientScript.
 */

/**
 * CClientScript manages JavaScript and CSS stylesheets for views.
 * This is a shortcut to its methods to write less code.
 */
class CCS {
  
  /**
   * Registers a core javascript library.
   * @param string the core javascript library name
   * @see renderCoreScript
   */
  static public function registerCoreScript($name) {
    return Yii::app()->getClientScript()->registerCoreScript($name);
  }

  /**
   * Registers a CSS file
   * @param string URL of the CSS file
   * @param string media that the CSS file should be applied to. If empty, it means all media types.
   */
  static public function registerCssFile($url,$media='') {
    return Yii::app()->getClientScript()->registerCssFile($url,$media);
  }

  /**
   * Registra un estilo que este en la carpeta /css/ pasandole la version para impedir cache.
   */
  static public function registerLocalCssFile($file, $media='') {
    return Yii::app()->getClientScript()->registerCssFile(self::getLocalCssUrl($file), $media);
  }

  static public function getLocalCssUrl($file) {
    $file_path = dirname(__FILE__).'/../../css/'.$file;
    $file_time = filemtime($file_path);
    $file_url = Yii::app()->request->baseUrl.'/css/'.$file.'?v='.$file_time;
    return $file_url;
  }

  /**
   * Registers a piece of CSS code.
   * @param string ID that uniquely identifies this piece of CSS code
   * @param string the CSS code
   * @param string media that the CSS code should be applied to. If empty, it means all media types.
   */
  static public function registerCss($id,$css,$media='') {
    return Yii::app()->getClientScript()->registerCss($id,$css,$media);
  }

  /**
   * Registers a javascript file.
   * @param string URL of the javascript file
   * @param integer the position of the JavaScript code. Valid values include the following:
   * <ul>
   * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
   * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
   * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
   * </ul>
   */
  static public function registerScriptFile($url,$position=CClientScript::POS_HEAD) {
    return Yii::app()->getClientScript()->registerScriptFile($url,$position);
  }

  /**
   * Registra un script que este en la carpeta /js/ pasandole la version para impedir cache.
   */
  static public function registerLocalScriptFile($file,$position=CClientScript::POS_HEAD) {
    return Yii::app()->getClientScript()->registerScriptFile(self::getLocalScriptUrl($file),$position);
  }

  static public function getLocalScriptUrl($file) {
    $file_path = dirname(__FILE__).'/../../js/'.$file;
    $file_time = filemtime($file_path);
    $file_url = Yii::app()->request->baseUrl.'/js/'.$file.'?v='.$file_time;
    return $file_url;
  }

  /**
   * Registers a piece of javascript code.
   * @param string ID that uniquely identifies this piece of JavaScript code
   * @param string the javascript code
   * @param integer the position of the JavaScript code. Valid values include the following:
   * <ul>
   * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
   * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
   * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
   * <li>CClientScript::POS_LOAD : the script is inserted in the window.onload() function.</li>
   * <li>CClientScript::POS_READY : the script is inserted in the jQuery's ready function.</li>
   * </ul>
   */
  static public function registerScript($id,$script,$position=CClientScript::POS_READY) {
    return Yii::app()->getClientScript()->registerScript($id,$script,$position);
  }

  /**
   * Registers a meta tag that will be inserted in the head section (right before the title element) of the resulting page.
   * @param string content attribute of the meta tag
   * @param string name attribute of the meta tag. If null, the attribute will not be generated
   * @param string http-equiv attribute of the meta tag. If null, the attribute will not be generated
   * @param array other options in name-value pairs (e.g. 'scheme', 'lang')
   * @since 1.0.1
   */
  static public function registerMetaTag($content,$name=null,$httpEquiv=null,$options=array()) {
    return Yii::app()->getClientScript()->registerMetaTag($content,$name,$httpEquiv,$options);
  }

  /**
   * Registers a link tag that will be inserted in the head section (right before the title element) of the resulting page.
   * @param string rel attribute of the link tag. If null, the attribute will not be generated.
   * @param string type attribute of the link tag. If null, the attribute will not be generated.
   * @param string href attribute of the link tag. If null, the attribute will not be generated.
   * @param string media attribute of the link tag. If null, the attribute will not be generated.
   * @param array other options in name-value pairs
   * @since 1.0.1
   */
  static public function registerLinkTag($relation=null,$type=null,$href=null,$media=null,$options=array()) {
    return Yii::app()->getClientScript()->registerLinkTag($relation,$type,$href,$media,$options);
  }

  /**
   * Checks whether the CSS file has been registered.
   * @param string URL of the CSS file
   * @return boolean whether the CSS file is already registered
   */
  static public function isCssFileRegistered($url) {
    return Yii::app()->getClientScript()->isCssFileRegistered($url);
  }

  /**
   * Checks whether the CSS code has been registered.
   * @param string ID that uniquely identifies the CSS code
   * @return boolean whether the CSS code is already registered
   */
  static public function isCssRegistered($id) {
    return Yii::app()->getClientScript()->isCssRegistered($id);
  }

  /**
   * Checks whether the JavaScript file has been registered.
   * @param string URL of the javascript file
   * @param integer the position of the JavaScript code. Valid values include the following:
   * <ul>
   * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
   * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
   * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
   * </ul>
   * @return boolean whether the javascript file is already registered
   */
  static public function isScriptFileRegistered($url,$position=CClientScript::POS_HEAD) {
    return Yii::app()->getClientScript()->isScriptFileRegistered($url,$position);
  }

  /**
   * Checks whether the JavaScript code has been registered.
   * @param string ID that uniquely identifies the JavaScript code
   * @param integer the position of the JavaScript code. Valid values include the following:
   * <ul>
   * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
   * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
   * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
   * <li>CClientScript::POS_LOAD : the script is inserted in the window.onload() function.</li>
   * <li>CClientScript::POS_READY : the script is inserted in the jQuery's ready function.</li>
   * </ul>
   * @return boolean whether the javascript code is already registered
   */
  static public function isScriptRegistered($id,$position=CClientScript::POS_READY) {
    return Yii::app()->getClientScript()->isScriptRegistered($id,$position);
  }
}

?>
